﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity.Model;

namespace DutyWeb.UserControls
{
    public partial class DepartmentTree : BaseUserControl
    {
        /// <summary>
        /// 
        /// </summary>
        private DepartmentManagement departmentManagement = new DepartmentManagement();

        /// <summary>
        /// 
        /// </summary>
        public Guid? SelectedDepartmentID
        {
            get
            {
                if (string.IsNullOrEmpty(this.txtSelectedNodeText.Text.Trim()))
                {
                    return null;
                }
                if (string.IsNullOrEmpty(this.hidSelectedDepartment.Value))
                {
                    return null;
                }
                else
                {
                    return new Guid(this.hidSelectedDepartment.Value);
                }
            }
            set
            {
                if (value == null)
                {
                    this.hidSelectedDepartment.Value = string.Empty;
                    this.txtSelectedNodeText.Text = string.Empty;
                }
                else
                {
                    this.hidSelectedDepartment.Value = value.ToString();
                }
                this.LoadDepartmentTree();
            }
        }

        public string SelectedDepartmentName
        {
            get
            {
                return this.txtSelectedNodeText.Text.Trim();
            }
            set
            {
                this.txtSelectedNodeText.Text = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string SelectedParentDNA
        {
            get
            {
                return this.hidSelectedParentDNA.Value;
            }
        }

        /// <summary>
        /// place the department nodes to this list
        /// </summary>
        private List<Department> treeNodeList;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.LoadDepartmentTree();
                this.departmentTree.ShowCheckBoxes = TreeNodeTypes.All;
            }

            //this.txtSelectedNodeText.Attributes.Add("onclick", string.Format("switchDisplaying('tt');selectedTheDepartmentRadio('{0}','xgameradio');this.disabled='disabled'", this.SelectedDepartmentID));
        }

        /// <summary>
        /// bind the left menu tree via the administrator's permission
        /// </summary>
        public void LoadDepartmentTree()
        {
            //pseudocode
            Sys_User sUser = new Sys_User();
            //sUser.UserGroupID = new Guid("48AE131B-4B52-E111-B979-001558309069");
            //sUser.IsSuperAdministrator = false;
            sUser.IsSuperAdministrator = true;
            //pls add the real sys_user info the instance above

            treeNodeList = this.departmentManagement.LoadDepartmentMenuTree(sUser);

            TreeNode tn = new TreeNode();
            Department dn = new Department();
            if (sUser.IsSuperAdministrator == false)
            {
                int min = 1000;
                foreach (Department d3 in treeNodeList)
                {
                    if (min > d3.ParentDNA.Length)
                    {
                        min = d3.ParentDNA.Length;
                    }
                }
                dn = treeNodeList.Where(d2 => d2.ParentDNA.Length == min).FirstOrDefault();
            }
            else
            {
                dn = treeNodeList.Where(d2 => d2.ParentID == null).FirstOrDefault();
            }

            //tn.Text = "请选择"; //dn.DepartmentName;
            //this.departmentTree.Nodes.Add(tn);
            if (treeNodeList == null)
            {
                return;
            }

            this.AppendChildNode(dn, tn);
        }

        /// <summary>
        /// recursion bind the tree view
        /// </summary>
        /// <param name="sltree"></param>
        /// <param name="matherNode"></param>
        private void AppendChildNode(Department d, TreeNode matherNode)
        {
            if (this.departmentTree.Nodes.Count == 0)
            {
                matherNode.ShowCheckBox = false;
                this.departmentTree.Nodes.Add(matherNode);
            }

            TreeNode tn = new TreeNode();
            tn.NavigateUrl = "javascript:doNothing();";
            string selected = string.Empty;
            if (this.SelectedDepartmentID == d.ID)
            {
                selected = "checked=checked";
            }
            tn.Text = string.Format("<input type=\"radio\" runat=\"server\" {4} onclick=selectThis('ctl00_ContentPlaceHolderContent_{3}_hidSelectedDepartment','ctl00_ContentPlaceHolderContent_{3}_hidSelectedParentDNA','ctl00_ContentPlaceHolderContent_{3}_txtSelectedNodeText','{0}','{2}','{1}');blockTheObject('tt') name=\"xgameradio\" value=\"{0}\" /> {1}", d.ID, d.DepartmentName, d.ParentDNA, this.ID, selected);
            tn.Value = d.ID.ToString();
            //tn.ShowCheckBox = true;
            matherNode.ChildNodes.Add(tn);

            List<Department> list = this.treeNodeList.Where(t => t.ParentID == d.ID && t.ParentID != null).ToList();

            foreach (Department d1 in list)
            {
                AppendChildNode(d1, tn);
            }
        }
    }
}